import 'package:bot_toast/bot_toast.dart';
import 'package:rfid/entity/lock_state_entity.dart';
import 'package:rfid/event/check_result_event.dart';
import 'package:rfid/event/user_id_event.dart';
import 'package:rfid/util/toast_util.dart';

import '../entity/inventory_entity.dart';
import '../event/id_event.dart';
import '../event/inventory_rfid_event.dart';
import '../event/lock_query_event.dart';
import '../event/lock_state_event.dart';
import '../main.dart';
import '../util/analysis_reader_rule.dart';
import '../util/convert_util.dart';
import 'three_to_one_manager.dart';

class ResultHandler{

  ThreeToOneManager manager = ThreeToOneManager();

  handler(data){
    var cmd = data['cmd'];
    var status = data['status'];
    print('cmd = $cmd, status = $status');

    if(cmd == 'locker_check' && status == 200){
      eventFactory.fire(CheckResultEvent('locker_check', data['data']['check']));
    }
    if(cmd == 'rfid_uhf_check' && status == 200){
      eventFactory.fire(CheckResultEvent('rfid_uhf_check', data['data']['check']));
    }
    if(cmd == 'card_reader_check' && status == 200){
      eventFactory.fire(CheckResultEvent('card_reader_check', data['data']['check']));
    }
    if(cmd == 'card_reader_m1_inventory' ){
      if(status == 200){
        String? uid =data['data']['uid'];
        if(uid != null ){
          var uidNo = analysisReaderRule(uid);
          // String exUid = uid.substring(6,8)+uid.substring(4,6)+uid.substring(2,4)+uid.substring(0,2);
          // String uidNo = hexToInt(exUid).toString();
          // while(uidNo.length<10){
          //   uidNo = '0$uidNo';
          // }
          eventFactory.fire(UserIdEvent(uidNo??'null'));
        }else{
          eventFactory.fire(UserIdEvent(uid??'null'));
        }
      }else{
        manager.start();
        // eventFactory.fire(UserIdEvent('null'));
      }
    }
    if(cmd == 'card_reader_icr_inventory' ){
      if(status == 200){
        eventFactory.fire(IdEvent(data['data']['no'],data['data']['name']));
      }else{
        manager.start();
        // eventFactory.fire(UserIdEvent('null'));
      }
    }
    if(cmd == 'locker_state_changed' && status == 200){
      eventFactory.fire(LockStateEvent(data['data']['addr'],data['data']['no'],data['data']['state']));
    }
    // if(cmd == 'locker_open' && status == 200){
    //   eventFactory.getEventBus().fire(LockOpenEvent());
    // }
    if(cmd == 'locker_query' && status == 200){
      eventFactory.fire(LockQueryEvent(LockStateEntity.fromJson(data['data'])));
    }
    if(cmd == 'rfid_uhf_scan' && status == 200){
      BotToast.closeAllLoading();
      eventFactory.fire(InventoryRfidEvent(InventoryEntity.fromJson(data['data'])));
    }
    if(cmd == 'rfid_hf_scan' && status == 200){
      BotToast.closeAllLoading();
      eventFactory.fire(InventoryRfidEvent(InventoryEntity.fromJson(data['data'])));
    }

    if((cmd == 'rfid_hf_scan' ||cmd == 'rfid_uhf_scan' ) && status == 400){
      BotToast.closeAllLoading();
      toastWarn(data['error']);
    }

  }

}